package com.lc.afbx.activity;

import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ListView;

import com.lc.afbx.BaseApplication;
import com.lc.afbx.R;
import com.lc.afbx.adapters.AddressAdapter;
import com.lc.afbx.conn.GetApiMineAddress;
import com.lc.afbx.util.MyToast;
import com.zcx.helper.app.AppCallBack;
import com.zcx.helper.http.AsyCallBack;

import java.util.ArrayList;
import java.util.List;

/**
 * Created by Administrator on 2016/5/23.
 */
public class AddressActivity extends BaseActivity implements View.OnClickListener {


    private ListView address_lv;
    private AddressAdapter adapter;
    private List<GetApiMineAddress.Data> lists = new ArrayList<>();

    //默认地址接口   好像没用//TODO
//    private GetApiDefaultAddress getApiDefaultAddress = new GetApiDefaultAddress(new AsyCallBack<GetApiDefaultAddress.Info>() {
//        @Override
//        public void onSuccess(String toast, int type, GetApiDefaultAddress.Info info) throws Exception {
//
//
//        }
//    });

    //地址接口
    private GetApiMineAddress getApiMineAddress = new GetApiMineAddress(new AsyCallBack<GetApiMineAddress.Info>() {
        @Override
        public void onSuccess(String toast, int type, GetApiMineAddress.Info info) throws Exception {

            lists.addAll(info.Data);

            adapter.notifyDataSetChanged();

//            UtilLog.e("这是adapter的长度", "" + adapter.getCount());
        }

        @Override
        public void onFail(String toast, int type) throws Exception {

            MyToast.showToast(toast, context);
        }
    });


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_geren_address);

        address_lv = ((ListView) findViewById(R.id.address_lv));

        address_lv.setDivider(null);

        address_lv.setAdapter(adapter = new AddressAdapter(context, lists));

        address_lv.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {

                Intent intent = new Intent(AddressActivity.this, AddressEditActivity.class);

                Bundle bundle = new Bundle();

                bundle.putSerializable("data", lists.get(position));

                intent.putExtras(bundle);

                startActivity(intent);

            }
        });

        setAppCallBack(new RefreshAddressCallback());

        new RefreshAddressCallback().refresh();
    }


    @Override
    public void onClick(View view) {


        switch (view.getId()) {

            case R.id.back:

                finish();
                return;

            case R.id.address_add_ll:

                startActivity(new Intent(this, AddAddressActivity.class));
                break;

        }


    }


    public class RefreshAddressCallback implements AppCallBack {

        public void refresh(){

            lists.clear();

            getApiMineAddress.userid = Integer.parseInt(BaseApplication.BasePreferences.readUserId());
            getApiMineAddress.execute(AddressActivity.this, false);
        }
    }


}
